package com.ruoyi.central.controller;

import java.util.Comparator;
import java.util.List;

import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralProductTypeMaterial;
import com.ruoyi.central.service.ITopCentralProductTypeMaterialService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 产品批次关联Controller
 * 
 * @author SunLuQuan
 * @date 2022-04-17
 */
@Controller
@RequestMapping("/central/centralProductTypeMaterial")
public class TopCentralProductTypeMaterialController extends BaseController
{
    private String prefix = "central/centralProductTypeMaterial";

    @Autowired
    private ITopCentralProductTypeMaterialService topCentralProductTypeMaterialService;

    @RequiresPermissions("central:centralProductTypeMaterial:view")
    @GetMapping()
    public String centralProductTypeMaterial()
    {
        return prefix + "/centralProductTypeMaterial";
    }

    /**
     * 查询产品批次关联列表
     */
    @RequiresPermissions("central:centralProductTypeMaterial:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralProductTypeMaterial topCentralProductTypeMaterial)
    {
        startPage();
        List<TopCentralProductTypeMaterial> list = topCentralProductTypeMaterialService.selectList(topCentralProductTypeMaterial);
        return getDataTable(list);
    }

    /**
     * 查询简单的产品批次关联列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralProductTypeMaterial topCentralProductTypeMaterial){

        Collection<TopCentralProductTypeMaterial> topCentralProductTypeMaterialList = topCentralProductTypeMaterialService.selectAllMap().values()
                .stream().sorted(Comparator.comparing(TopCentralProductTypeMaterial::getCreateTime).reversed()).collect(Collectors.toList());
        return AjaxResult.success(SelectUtils.buildSelectList(topCentralProductTypeMaterialList,TopCentralProductTypeMaterial::getBatchNo,TopCentralProductTypeMaterial::getProductTypeId));
    }

    /**
     * 获取产品批次关联详细信息
     */
    @RequiresPermissions("central:centralProductTypeMaterial:query")
    @GetMapping(value = "/{batchNo}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("batchNo") String batchNo)
    {
        return AjaxResult.success(topCentralProductTypeMaterialService.selectById(batchNo));
    }

    /**
     * 获取自增排序值
     * @param topCentralProductTypeMaterial 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralProductTypeMaterial topCentralProductTypeMaterial){
        int weigth = topCentralProductTypeMaterialService.getMaxOfWeight(topCentralProductTypeMaterial);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载产品批次关联导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralProductTypeMaterial> util = new ExcelUtil<>(TopCentralProductTypeMaterial.class);
        util.importTemplateExcel(response, "产品批次关联数据");
    }

    /**
     * 导入产品批次关联数据
     * @param file 产品批次关联数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "产品批次关联", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralProductTypeMaterial:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralProductTypeMaterial> util = new ExcelUtil<>(TopCentralProductTypeMaterial.class);
        List<TopCentralProductTypeMaterial> topCentralProductTypeMaterialList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralProductTypeMaterialService.updateBatchById(topCentralProductTypeMaterialList)){
                return AjaxResult.success("导入产品批次关联数据覆盖成功！");
            }
            return AjaxResult.error("导入产品批次关联数据覆盖失败！");
        }
        if(topCentralProductTypeMaterialService.insertBatch(topCentralProductTypeMaterialList)){
            return AjaxResult.success("导入产品批次关联数据新增成功！");
        }
        return AjaxResult.error("导入产品批次关联数据新增失败！");
    }

    /**
     * 导出产品批次关联列表
     */
    @RequiresPermissions("central:centralProductTypeMaterial:export")
    @Log(title = "产品批次关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralProductTypeMaterial topCentralProductTypeMaterial)
    {
        List<TopCentralProductTypeMaterial> list = topCentralProductTypeMaterialService.selectList(topCentralProductTypeMaterial);
        ExcelUtil<TopCentralProductTypeMaterial> util = new ExcelUtil<TopCentralProductTypeMaterial>(TopCentralProductTypeMaterial.class);
        return util.exportExcel(list, "产品批次关联数据");
    }

    /**
     * 新增产品批次关联
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品批次关联
     */
    @RequiresPermissions("central:centralProductTypeMaterial:add")
    @Log(title = "产品批次关联", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralProductTypeMaterial topCentralProductTypeMaterial)
    {
        return toAjax(topCentralProductTypeMaterialService.insert(topCentralProductTypeMaterial));
    }

    /**
     * 修改产品批次关联
     */
    @RequiresPermissions("central:centralProductTypeMaterial:edit")
    @GetMapping("/edit/{batchNo}")
    public String edit(@PathVariable("batchNo") String batchNo, ModelMap mmap)
    {
        TopCentralProductTypeMaterial topCentralProductTypeMaterial = topCentralProductTypeMaterialService.selectById(batchNo);
        mmap.put("topCentralProductTypeMaterial", topCentralProductTypeMaterial);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品批次关联
     */
    @RequiresPermissions("central:centralProductTypeMaterial:edit")
    @Log(title = "产品批次关联", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralProductTypeMaterial topCentralProductTypeMaterial)
    {
        return toAjax(topCentralProductTypeMaterialService.updateById(topCentralProductTypeMaterial));
    }

    /**
     * 删除产品批次关联
     */
    @RequiresPermissions("central:centralProductTypeMaterial:remove")
    @Log(title = "产品批次关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{batchNos}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("batchNos") List<String> batchNos)
    {
            return toAjax(topCentralProductTypeMaterialService.deleteByIds(batchNos));
    }
}
